<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/10/10
 * Time: 11:36
 * 问题对标（客户网点来对存在的问题反馈）
 */
class QuestionBenchmark extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'question_benchmark';
    }

    //isreturn 申请还分状态，前台用户申请，后台用户操作是否还分
    const BENCHMARK_SCORE_APPLYRETURN = 1;//申请还分
    const BENCHMARK_SCORE_NOTEAPPLY = 0;//不申请还分
    const BENCHMARK_SCORE_RETURED = 2;//已经还分
    /**
     * 获取每一个题目的对标信息
     */
    public function getBySubject($nq_id, $subject_id){
        $criteria = new CDbCriteria();
        $criteria->addColumnCondition(array('nq_id' => $nq_id));
        $criteria->addColumnCondition(array('subject_id' => $subject_id));
        $criteria->order = 'create_time asc';
        $data = $this->findAll($criteria);
        if ($data) {
            $result = array();
            $network = new NetWork();
            foreach ($data as $key => $val) {
                $result[$key] = $val;
                $network_id = $val['network_id'];
                $info = $network->findByPk($network_id)->attributes;
                $result[$key]['network_name'] = $info['network_name'];
            }
            return $result;
        }
        return $data;
    }
    /**
     * 得到问卷的所有对标信息
     * 只有本网点有异议需要审核的题目
     */
    public function getByNq($nq_id)
    {
        $data = $this->findAll("nq_id={$nq_id} and is_self=1");
        if ($data) {
            $result = array();
            foreach ($data as $key => $val) {
                $subject_id = $val['subject_id'];
                $result[$subject_id] = $val;
            }
            return $result;
        }
        return $data;        
    }
    /**
     * 得到这个网点的所有历史对标信息
     * @param $current_network_id 当前登录网点的id，主要是用来判断当前用户是否有对此网点问卷有申请还分的权利
     */
    public function getNqList($nq_id){
        $data = $this->findAll("nq_id={$nq_id}");

        if ($data) {
            $result = array();
            foreach ($data as $key => $val) {
                $subject_id = $val['subject_id'];
                $result[$subject_id][$key] = $val;
                if ($val['is_self']==1) {
                    $result[$subject_id]['shenqing'] = 1;
                    $result[$subject_id]['suggestion'] = $val['suggestion'];
                }
            }
            return $result;
        }
        return $data;  
    }
}